import cn.hutool.json.JSONUtil;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.moderation.v2.ModerationClient;
import com.huaweicloud.sdk.moderation.v2.model.*;
import com.huaweicloud.sdk.moderation.v2.region.ModerationRegion;

import java.util.ArrayList;
import java.util.List;

public class RunTextModerationSolution {

    public static void main(String[] args) {
        String ak = "HYA7PFYVSZSJ3BO8WN03";
        String sk = "InNGxyn2bcSy75Pa5RK4oyo0JD0DbKM9MRi1XjId";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        ModerationClient client = ModerationClient.newBuilder()
                .withCredential(auth)
                .withRegion(ModerationRegion.valueOf("cn-east-3"))
                .build();

        RunTextModerationRequest request = new RunTextModerationRequest();
        TextDetectionReq body = new TextDetectionReq();
        List<TextDetectionItemsReq> listbodyItems = new ArrayList<>();
        listbodyItems.add(
            new TextDetectionItemsReq()
                //.withText("666666luo聊请+110亚砷酸钾六位qqweixin,fuck666666666666666")
                .withText("666666666666666666666")
                .withType("content")
        );
        List<String> listbodyCategories = new ArrayList<>();
        listbodyCategories.add("ad");
        listbodyCategories.add("politics");
        listbodyCategories.add("abuse");
        listbodyCategories.add("porn");
        listbodyCategories.add("contraband");
        listbodyCategories.add("flood");
        body.withItems(listbodyItems);
        body.withCategories(listbodyCategories);
        request.withBody(body);
        try {
            RunTextModerationResponse response = client.runTextModeration(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(JSONUtil.parse(response));
            String suggestion = response.getResult().getSuggestion();
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
